package com.kuang.singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//单例模式
//静态内部类实现
public class SingletonDemo04 {
    private SingletonDemo04(){
    }

    private static class InnerClass{
        private static SingletonDemo04 instance= new SingletonDemo04();
    }

    public static SingletonDemo04 getInstance(){
        return InnerClass.instance;
    }
}

//反射机制可以破坏上面这个以及上面的前四种单例模式
class SingletonDemo04Test{
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        SingletonDemo04 instance = SingletonDemo04.getInstance();

        Constructor<SingletonDemo04> declaredConstructor = SingletonDemo04.class.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        SingletonDemo04 instance2 = declaredConstructor.newInstance();

        System.out.println(instance==instance2);//false
    }
}